package db

import (
	"go-study/src/sjcc/models"
	"gorm.io/gorm"
	"log"
)

// 创建  手动关闭连接
func InsertDepartment(db *gorm.DB, department models.Department) {
	connection, err := db.DB()
	if err != nil {
		log.Fatalf("failed to connect database: %v", err)
	}
	db.Create(&department)
	closeErr := connection.Close()
	if err != nil {
		log.Println("close collection is error" + closeErr.Error())
	}
}

// 批量保存
func BatchInsertDepartment(db *gorm.DB, departments []models.Department) {
	connection, err := db.DB()
	if err != nil {
		log.Fatalf("failed to connect database: %v", err)
	}
	db.Create(&departments)
	closeErr := connection.Close()
	if err != nil {
		log.Println("close collection is error" + closeErr.Error())
	}
}

// 查询
func QueryDepartment(db *gorm.DB) []models.Department {
	// 读取
	var departments []models.Department
	return departments
}

// 更新
func UpdateDepartment(db *gorm.DB, department models.Department) {
	// 更新
	db.Model(&department).Updates(models.Department{})
}

// 删除
func DeleteDepartment(db *gorm.DB, school models.Department) {
	// 删除
	db.Delete(&models.Department{}, 1)
}
